package SpanishExercise;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyAdapter;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.util.regex.*;
public class SpanishNumberExercise implements ActionListener,KeyListener {
	JFrame exerciseFrame;
	JPanel questionPane,infoPane,mainPane,hintPane,messagePane,messageButtonPane;
	JTextField answer;
	JLabel question, result, scoreLabel,levelLabel, errorLabel,messageLabel;
	JLabel[] hintLabel;
	JButton messageButton;
	TitledBorder title,infoTitle,hintTitle;
	int level,score,errorCount;
	int[] levelSet={6,11,16,21,26,31,36,41,46,51,61,81,101,200,300,1000,2000,3000,10000,20000,30000,100000,
		200000,300000,1000000};
	boolean accuteAccent;
	int secret,oldSecret;
	Random random;
	Pattern pattern;
	public SpanishNumberExercise() {
		random=new Random();
		pattern=Pattern.compile("'[aeiou]");
		resetLevel();
		exerciseFrame = new JFrame("Spanish Number Excercise");
		addMainPane();
		exerciseFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		exerciseFrame.pack();
		exerciseFrame.setVisible(true);
		addMessagePane();
		mainPane.setVisible(false);
	}
	private void addMessagePane(){
		messagePane = new JPanel();
		messageButtonPane=new JPanel();
		messagePane.setLayout(new GridLayout(2,1));
		messageButton = new JButton("Start Exercise");
		messageButton.addActionListener(this);
		messageLabel = new JLabel("Welcome to Spanish Number Exercise!",SwingConstants.CENTER);
		messagePane.add(messageLabel);
		messagePane.add(messageButtonPane);
		messageButtonPane.add(messageButton);
		exerciseFrame.getContentPane().add(messagePane, BorderLayout.CENTER);
	}
	private void addMainPane(){
		questionPane = new JPanel();
		infoPane = new JPanel();
		mainPane = new JPanel();
		hintPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
		addHintWidgets();
		addInfoWidgets();
		addQuestionWidgets();
		mainPane.add(infoPane);
		mainPane.add(hintPane);
		mainPane.add(questionPane);
		exerciseFrame.getContentPane().add(mainPane, BorderLayout.CENTER);
		setQuestion();
	}
	private void addHintWidgets(){
		hintTitle = BorderFactory.createTitledBorder("Hints");
		hintPane.setBorder(hintTitle);
		hintPane.setLayout(new GridLayout(5,1));
		hintLabel=new JLabel[5];
		for(int i=0;i<5;i++){
			hintLabel[i] = new JLabel("",SwingConstants.LEFT);
			hintPane.add(hintLabel[i]);
		}
	}
	private void addInfoWidgets(){
		infoTitle = BorderFactory.createTitledBorder("Status");
		infoPane.setLayout(new GridLayout(1,3));
		infoPane.setBorder(infoTitle);
		scoreLabel = new JLabel("",SwingConstants.LEFT);
		levelLabel = new JLabel("",SwingConstants.LEFT);
		errorLabel = new JLabel("",SwingConstants.LEFT);
		infoPane.add(levelLabel);
		infoPane.add(scoreLabel);
		infoPane.add(errorLabel);
	}
	private void addQuestionWidgets() {
		title = BorderFactory.createTitledBorder("Exercise");
		questionPane.setBorder(title);
		questionPane.setLayout(new GridLayout(4,1));
		question = new JLabel("", SwingConstants.LEFT);
		answer = new JTextField(16);
		result = new JLabel("", SwingConstants.LEFT);
		answer.addKeyListener(this);
		answer.addActionListener(this);
		questionPane.add(question);
		questionPane.add(answer);
		questionPane.add(result);
		question.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		result.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		
	}
	private void resetLevel(){
		level=1;
		score=0;
		errorCount=0;
	}
	public void keyReleased(KeyEvent e) {
        JTextField textField = (JTextField) e.getSource();
        String text = textField.getText();
        if(pattern.matcher(text).find()){
	    	answer.setText(answer.getText()
	        	.replaceAll("[']a","á")
	        	.replaceAll("[']e","é")
	    	    .replaceAll("[']i","í")
	        	.replaceAll("[']o","ó")
	        	.replaceAll("[']u","ú")
	        );
	    }
	}
	public void keyPressed(KeyEvent e){}
	public void keyTyped(KeyEvent e){}
	public int nextNumber(){
		int retValue;
		if(level==1){
			retValue=random.nextInt(levelSet[level-1]);
		}else{
			if(random.nextInt(10)>7){
				retValue=random.nextInt(levelSet[level-2]);
			}else{
				retValue=levelSet[level-2]+random.nextInt(levelSet[level-1]-levelSet[level-2]);
			}
		}
		return retValue;
	}
	private void setQuestion(){
		level=score/10+1;
		levelLabel.setText("Level: "+level);
		scoreLabel.setText("Score: "+score);
		errorLabel.setText("Error: "+errorCount);
		int tempSecret;
		do{
			tempSecret=nextNumber();
		}while(secret==tempSecret || oldSecret==tempSecret);
		oldSecret=secret;
		secret=tempSecret;
		boolean duplicated;
		int[] temp={0,1,2,3,4};
		for(int i=0;i<5;i++){
			if(level<3 && i>2){
				hintLabel[3].setText("");
				hintLabel[4].setText("");
				break;
			}
			do{
				duplicated=true;
				temp[i]=nextNumber();
				if(secret!=temp[i]){
					duplicated=false;
					for(int j=0;j<i;j++){
						if(temp[j]==temp[i])
							duplicated=true;
					}
				}
			}while(duplicated);
			hintLabel[i].setText(SpanishNumber.toString(temp[i]));
		}
		if(level<3){
			hintLabel[random.nextInt(3)].setText(SpanishNumber.toString(secret));
		}else{
			hintLabel[random.nextInt(5)].setText(SpanishNumber.toString(secret));
		}
		question.setText("Type the spanish for "+secret+" then press 'enter' (type 'a,'e,'i,'o,'u for á,é,í,ó,ú)?          ");
		answer.setText("");
	}
	public void actionPerformed(ActionEvent event) {
		if(event.getSource()==answer){
			String ans=answer.getText().trim();
			if(ans.equals(SpanishNumber.toString(secret))){
				result.setText("You are correct!");
				score++;
			}else{
				result.setText(secret+"="+SpanishNumber.toString(secret));
				errorCount++;
				if(errorCount>=3) gameOver(); 
			}
			setQuestion();
		}else if(event.getSource()==messageButton){
			startExercise();
			mainPane.setVisible(true);
			messagePane.setVisible(false);
		}
	}
	private void gameOver(){
		mainPane.setVisible(false);
		messageLabel.setText("Game over! Your score is "+score);
		messagePane.setVisible(true);
	}
	private void startExercise(){
		resetLevel();
		setQuestion();
		result.setText("");
	}
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		} catch (Exception e) {}
		SpanishNumberExercise exercise = new SpanishNumberExercise();
	}
}
